package com.example.basemvp.base;

import android.util.Log;

import java.lang.ref.WeakReference;

public abstract class BasePresenter<V extends BaseView, M extends BaseModel, C> {
    private static final String TAG = "BasePresenter";
    protected WeakReference<V> weakReference;
    protected M model;

    protected abstract M getModel();

    protected abstract C getContract();

    public BasePresenter() {
        model = getModel();
    }

    protected V getView() {
        if (weakReference != null) {
            return weakReference.get();
        }
        return null;

    }

    public void bindView(V view) {
        //使用弱应用
        weakReference = new WeakReference<>(view);
        Log.e(TAG, "bindView: " + weakReference);

    }

    //手动gc 让系统回收，以免内存泄漏
    public void unBindView() {
        Log.e(TAG, "unBindView: ");
        if (weakReference != null) {
            weakReference.clear();
            weakReference = null;
        }
        System.gc();
    }


}
